#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "defines.h"
#include "readini.h"
#include "cores.h"

static CoreCtx *pCtx = NULL;

int main(int argc, char** argv){
    pj_status_t status = -1;

    if (argc < 2) {
        printf("Usage: %s <config file>\n", argv[0]);
        return -1;
    }

    pCtx = malloc(sizeof(CoreCtx));
    if (pCtx == NULL) {
        return -1;
    }

    memset(pCtx, 0, sizeof(CoreCtx));

    readIni(argv[1], &pCtx->account, &pCtx->settings);
    printAccount(&pCtx->account);
    printSettings(&pCtx->settings);

    status = initClient(pCtx);
    if (status != PJ_SUCCESS) {
        perror("Error initializing client");
        return -1;
    }

    pjmedia_aud_dev_info aud_dev_info[PJMEDIA_AUD_MAX_DEVS];
	int count = PJMEDIA_AUD_MAX_DEVS;
	pjsua_enum_aud_devs(aud_dev_info, &count);
    printf("audio device count:%d\n", count);
	for (int i = 0; i < count; i++) {
		printf("audio device name:%s\n", aud_dev_info[i].name);
	}

    status = addAccount(pCtx);
    if (status != PJ_SUCCESS) {
        perror("Error adding account");
        return -1;
    }

    // get a string of number from console
    char pNumber[MAX_STRING_LENGTH] = {0};
    printf("Enter number: ");
    fgets(pNumber, MAX_STRING_LENGTH, stdin);
    pNumber[strcspn(pNumber, "\n")] = 0;

    status = makeCall(pCtx, pNumber);
    if (status != PJ_SUCCESS) {
        perror("Error making call");
        return -1;
    }

    printf("Press enter to exit\n");
    getchar();

    return 0;
}
